<?php
include("config.php");
include("include/db_connect.php");
include("include/session.php");
include("include/common.php");
include("include/item.php");
include("include/search.php");
include("include/payment.php");

if(isset($_SESSION['user_id'])) {
	if(isset($_REQUEST['item_id'])) {
		if(isset($_REQUEST['points'])) {
			
		} else {
			$item= getItem($_REQUEST['item_id']);
			//array(item name, description, category name, cost, seller id, seller name)
			if($item[0] == 0) {
				$pay_key = buy_item ($_REQUEST['item_id'], $_SESSION['user_id'], $item[4], $item[3]);		
				if($pay_key === false ) {
					get_page("message", array("title" => "Error!", "message" => "Try again!"));
				} else {
					updateKey($_REQUEST['item_id'], $pay_key);
					get_page("message", array("redirect" => "https://www.sandbox.paypal.com/webscr&cmd=_ap-payment&paykey=$pay_key"));
				}
			} else {
				get_page("index", array("name" => $userInfo[0], "error" => "Item is not for sale!"));
			}
		}
	} else {
		$userInfo = getUserInfo($_SESSION['user_id']);
		get_page("get_credits", array("name" => $userInfo[0]));
	}
} else {
	get_page("message", array("redirect" => "index.php"));
}
?>
